package com.bcx.crm.spring.aop.customAop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * Created by ys on 2018/4/6.
 *
 * 编写切面
 */
@Aspect
@Component
public class LogAspect {
    @Pointcut("@annotation(com.bcx.crm.spring.aop.customAop.MyAction)")
    public void annotationPointCut() {

    }

    @After(value = "annotationPointCut()")
    public void after(JoinPoint joinPoint){
        MethodSignature signature =(MethodSignature) joinPoint.getSignature();//获取切点的方法及其信息(切点所在的类)
        Method method = signature.getMethod();
        MyAction myAction = method.getAnnotation(MyAction.class);
        System.out.println("注解式拦截--注解定义的属性值是 : "+myAction.name());
    }

    @Pointcut("execution(* com.bcx.crm.spring.aop.customAop.DemoMethodService.*(..))")
    public void beforePointCut(){}

    @Before("beforePointCut()")
    public void before(JoinPoint joinPoint){
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        System.out.println("拦截到的方法名是 : "+method.getName());
    }

}
